package com.docMan.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.docMan.dao.BaseDao;
import com.docMan.dao.TchumanDao;
import com.docMan.dao.TcunitDao;
import com.docMan.dao.pojo.Tchuman;
import com.docMan.dao.pojo.Tcunit;
import com.docMan.service.TcunitService;
import com.docMan.utils.CastUtil;
import com.docMan.utils.FinalConfig;
import com.docMan.utils.ProjectUtils;

@Path("/tcunit")
@Produces(MediaType.APPLICATION_JSON)
@Service("tcunitService")
public class TcunitServiceImpl extends BaseServiceImpl<Tcunit> implements
		TcunitService {
	private TcunitDao tcunitDao;
	private TchumanDao tchumanDao;
	private HttpSession session;
	@Resource(name="tcunitDao")
	@Override
	public void setDao(BaseDao<Tcunit> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}

	@Autowired
	public void setSession(HttpSession session) {
		this.session = session;
	}
	@Autowired
	public void setTchumanDao(TchumanDao tchumanDao) {
		this.tchumanDao = tchumanDao;
	}

	public void setTcunitDao(TcunitDao tcunitDao) {
		this.tcunitDao = tcunitDao;
	}
	@Override
	@GET
	@Path("/getUnits")
	public String getUnits() {
		List<Tcunit> list = tcunitDao.findTcunit();
		String hql = "select h from Tchuman h where h.tcunitId in ( select t.unitId from Tcunit t where t.seniorUnitId=?)";
		for (Tcunit tcunit : list) {
			tcunit.getHumans().addAll(
					tchumanDao.find(ProjectUtils.getMemcachedKey(hql), hql,
							tcunit.getUnitId()));
		}
		return ProjectUtils.gson.toJson(list);
	}

	@Override
	@POST
	@Path("/getUnitByHumanId")
	public String getUnitByHumanId(@FormParam("humanId") int humanId) {
		String hqlString = "select u from Tcunit u where u.unitId in (select t.tcunitId from Tchuman t where t.humanId=?)";
		humanId=(int) session.getAttribute(FinalConfig.SESSION_HUMANID);
		List<Tcunit> list = tcunitDao.find(
				ProjectUtils.getMemcachedKey(hqlString, humanId), hqlString,
				humanId);

		return ProjectUtils.gson.toJson(list);
	}

	@Override
	public Tcunit findUnit() {
		List<Tcunit> list = findAll("select u from Tcunit u order u.unitId");
		Tcunit tcunit = null;
		for (Tcunit u : list) {
			if (u.getUnitId().intValue() == 1) {
				tcunit = u;
			}
		}
		list.remove(tcunit);
		tcunit = addUnit(tcunit, 1, list);
		return tcunit;
	}
	
	//�ݹ�����������ŵ����ι�ϵ��
	protected Tcunit addUnit(Tcunit tcunit, int level, List<Tcunit> tcunits) {

		List<Tcunit> subTcunits = tcunit.getSubTcunits();
		List<Tcunit> tempTcunits = new ArrayList<Tcunit>();
		int upId = tcunit.getUnitId();
		int addCount = 0;
		for (Tcunit u : tcunits) {
			if (u.getSeniorUnitId().intValue() == upId) {
				tempTcunits.add(u);
				addCount++;
			}
		}
		for (Tcunit u : tempTcunits) {
			subTcunits.add(addUnit(u, 2, tcunits));
		}
		System.out.println(addCount);
		tcunit.setSubTcunits(subTcunits);
		return tcunit;
	}

	@Override
	public int getUnitTopId(int unit) {
		return 0;
	}

	@Path("/getTopUnits")
	@GET
	@Override
	public String getTopUnits() {
		List<Tcunit> tcunits=find("select t from Tcunit t where t.seniorUnitId=?", 1);
		clear();
		int unitId=CastUtil.castInt(session.getAttribute(FinalConfig.SESSION_HUMAN_UNIT_ID));
		for (int i = 0; i < tcunits.size(); i++) {
			if(tcunits.get(i).getUnitId()==unitId){
				Tcunit tempUnit=tcunits.get(0);
				tcunits.set(0, tcunits.get(i));
				tcunits.set(i, tempUnit);
			}
		}
		put("units", tcunits);
		return getMap();
	}

	@Override
	@GET
	@Path("/getSubUnitsAndHumans/{unitId}")
	public String getSubUnits(@PathParam("unitId")int unitId) {
		List<Tcunit> tcunits=find("select t from Tcunit t where t.seniorUnitId=?",unitId);
		List<Tchuman> tchumans=tchumanDao.findNoCache("select h from Tchuman h where h.unitid=?", unitId);
		put("units", tcunits);
		put("humans", tchumans);
		return getMap();
	}
	
}
